## functions used in running models HPBM
# 2SLS ---------
run.IV.agency.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  if(type=="items"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ PCPI+ unemployment+Population+Murder_ArrestRate_lag +
                              Rape_ArrestRate_lag+Robbery_ArrestRate_lag+Burglary_ArrestRate_lag+
                              Assault_ArrestRate_lag', '| g+year',
                              '|(log(1+sumallLESOquantityHARRIS_lag)~SumUSitems_lag+zHUdI1_lag+ 
                              zHUdIland_lag+zHUdI6_lag+zHUdIhidta_lag)|g'))
  }else if(type=="value"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ PCPI+ unemployment+Population+Murder_ArrestRate_lag+
                              Rape_ArrestRate_lag+Robbery_ArrestRate_lag+Burglary_ArrestRate_lag+Assault_ArrestRate_lag',
                              '| g+year',
                              '|(log(1+sumallLESOaidHARRIS_lag)~SumUSvalue_lag+zHUd1_lag+zHUd6_lag+
                              zHUdland_lag+zHUdhitda_lag)|g'))
  }
  
  data %>% felm(form,data=.)
}


## County level -------
run.IV.county.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  county_vars_all_regressions <- paste(c("PCPI","unemployment","Population", "MURDER_ArrestRate_lag",
                                         "RAPE_ArrestRate_lag","ROBBERY_ArrestRate_lag","AGASSLT_ArrestRate_lag",
                                         "BURGLRY_ArrestRate_lag","OTHASLT_ArrestRate_lag","WEAPONS_ArrestRate_lag",
                                         "DRGSALE_ArrestRate_lag","DRGPOSS_ArrestRate_lag"),collapse='+')
  
  fe_variables_items <- paste('| g+year',
                              '|(log(1+sumallLESOquantityHARRIS_lag)~SumUSitems_lag+zHUdI1_lag+ 
                              zHUdIland_lag+zHUdI6_lag+zHUdIhidta_lag)|g')
  fe_variables_value <- paste('| g+year',
                              '|(log(1+sumallLESOaidHARRIS_lag)~SumUSvalue_lag+zHUd1_lag+zHUd6_lag+
                              zHUdland_lag+zHUdhitda_lag)|g')
  if(type=="items"){
    form <-  as.formula(paste(quo_text(outcome),'~',county_vars_all_regressions,fe_variables_items))
  }else if(type=="value"){
    form <-  as.formula(paste(quo_text(outcome),'~',county_vars_all_regressions,fe_variables_value))
  }
  
  data %>% felm(form,data=.)
}

## replication of Harris -----
run.IV.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  lagall_cols <- grep("^lagall",colnames(data))
  if(type=="items"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                              paste(colnames(data)[lagall_cols], collapse='+'),
                              '| fips+year',
                              '|(log_sum_items_lag~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                              zHUdI6_lag+zHUdIhidta_lag)|fips'))
  }else if(type=="value"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ PCPI+ UnemploymentRate+Pop+econmiss+', 
                              paste(colnames(data)[lagall_cols], collapse='+'),
                              '| fips+year',
                              '|(log_sum_value_ttl_lag~SumUSvalue_lag+
                              zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag)|fips'))
  }
  
  data %>% felm(form,data=.,keepX=T,keepCX=T)
}

## reduced form
reduced.agency.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  if(type=="items"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag+
                              zHUdI6_lag+zHUdIhidta_lag + PCPI+ unemployment+Population+Murder_ArrestRate_lag +
                              Rape_ArrestRate_lag+Robbery_ArrestRate_lag+Burglary_ArrestRate_lag+
                              Assault_ArrestRate_lag',
                              '| g+year',
                              '|0|g'))
  }else if(type=="value"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ SumUSvalue_lag+zHUd1_lag+zHUd6_lag+
                              zHUdland_lag+zHUdhitda_lag + PCPI+ unemployment+Population+Murder_ArrestRate_lag +
                              Rape_ArrestRate_lag+Robbery_ArrestRate_lag+Burglary_ArrestRate_lag+
                              Assault_ArrestRate_lag',
                              '| g+year',
                              '|0|g'))
  }
  
  data %>% felm(form,data=.)
}

reduced.county.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  if(type=="items"){
    form <- as.formula(paste(quo_text(outcome), '~SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag+zHUdI6_lag+zHUdIhidta_lag+
                             PCPI + unemployment + Population + MURDER_ArrestRate_lag+
                             RAPE_ArrestRate_lag+ROBBERY_ArrestRate_lag+AGASSLT_ArrestRate_lag+
                             BURGLRY_ArrestRate_lag+OTHASLT_ArrestRate_lag+WEAPONS_ArrestRate_lag+
                             DRGSALE_ArrestRate_lag+DRGPOSS_ArrestRate_lag','|g+year|0|g'))
    
  }else if(type=="value"){
    form <- as.formula(paste(quo_text(outcome), '~SumUSvalue_lag+zHUd1_lag+zHUd6_lag+zHUdland_lag+zHUdhitda_lag+
                             PCPI + unemployment + Population + MURDER_ArrestRate_lag+
                             RAPE_ArrestRate_lag+ROBBERY_ArrestRate_lag+AGASSLT_ArrestRate_lag+
                             BURGLRY_ArrestRate_lag+OTHASLT_ArrestRate_lag+WEAPONS_ArrestRate_lag+
                             DRGSALE_ArrestRate_lag+DRGPOSS_ArrestRate_lag','|g+year|0|g'))
  }
  
  data %>% felm(form,data=.)
  }

reduced.Harris <- function(outcome,data,type){
  outcome <- enexpr(outcome)
  lagall_cols <- grep("^lagall",colnames(data))
  if(type=="items"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ SumUSitems_lag+zHUdI1_lag+ zHUdIland_lag +
                              zHUdI6_lag+zHUdIhidta_lag + PCPI+ UnemploymentRate+Pop+econmiss+', 
                              paste(colnames(data)[lagall_cols], collapse='+'),
                              '| fips+year',
                              '|0|fips'))
  }else if(type=="value"){
    form <-  as.formula(paste(quo_text(outcome),
                              '~ SumUSvalue_lag+
                              zHUd1_lag+zHUd6_lag +zHUdland_lag+ zHUdhitda_lag + PCPI+ UnemploymentRate+Pop+econmiss+', 
                              paste(colnames(data)[lagall_cols], collapse='+'),
                              '| fips+year',
                              '|0|fips'))
  }
  
  data %>% felm(form,data=.)
}
